package com.tempjob.app.controller;

import com.alibaba.fastjson.JSON;
import com.tempjob.app.security.annotation.UserPassportSSO;
import com.tempjob.common.businesss.mongo.entity.IndustryCategory;
import com.tempjob.common.businesss.mongo.entity.JobCategory;
import com.tempjob.common.businesss.mongo.entity.SysDict;
import com.tempjob.common.businesss.service.MongoBeanUtil;
import com.tempjob.common.model.KeyValueModel;
import com.tempjob.common.vo.admin.UploadFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author hui
 **/
@RequestMapping("/sys/common")
@RestController
@Api(tags = "公共接口")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class CommonController {

    @Autowired
    private MongoBeanUtil mongoBeanUtil;

    @PostMapping("industryList")
    @ApiOperation(value = "获取行业分类")
    public Object industryList() {
        List<IndustryCategory> allList = mongoBeanUtil.getIndustryCategoryService().lambdaQuery().list();
        return allList;
    }

    @GetMapping("/code/{code}")
    @ApiOperation(value = "根据code获取字典数据")
    public Object getByCode(@PathVariable String code) {
        return mongoBeanUtil.getSysDictService().lambdaQuery().eq(SysDict::getCode, code).list();
    }


    @PostMapping(value = "/uploadOss")
    @ApiOperation(value = "oss上传接口", response = UploadFileVO.class)
    @UserPassportSSO
    public Object uploadOss(@RequestParam(value = "file") MultipartFile file) {
        return mongoBeanUtil.getOssUploadService().upload(file);
    }

    @GetMapping("categoryTree")
    @ApiOperation(value = "获取工作分类树形")
    public Object categoryTree() {
        String categoryKey = "xishu:tempjob:jobCategory";
        String categoryString = mongoBeanUtil.getRedisTemplate().opsForValue().get(categoryKey);
        if (StringUtils.isNotBlank(categoryString)) {
            List<KeyValueModel> allList = JSON.parseArray(categoryString, KeyValueModel.class);
            return allList;
        }
        List<JobCategory> allList = mongoBeanUtil.getJobCategoryService().list();
        List<KeyValueModel> tree = mongoBeanUtil.getJobCategoryService().buildTree(allList);
        mongoBeanUtil.getRedisTemplate().opsForValue().set(categoryKey, JSON.toJSONString(tree), 1, TimeUnit.DAYS);
        return tree;
    }


}
